<template>
  <div>
    <div>
        <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" title="编辑成员信息" width="500px">
          <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
            <el-form-item label="成员名称" prop="agentName">
              <el-input v-model="form.agentName" style="width: 350px;" />
            </el-form-item>
            <el-form-item label="手机号码" prop="phone">
                <el-select v-model="form.phone" style="width: 350px" 
                    placeholder="请选择手机号码" @change="getMemberUser" clearable filterable>
                  <el-option
                  v-for="(item, index) in users"
                  :key="item.phone + index"
                  :label="item.phone"
                  :value="item.phone"
                  />
                </el-select>
            </el-form-item>
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" style="width: 350px;" />
            </el-form-item>
            <el-form-item label="联系地址" prop="address">
              <el-input v-model="form.address" style="width: 350px;" />
            </el-form-item>
            <el-form-item label="分成比例(%)" prop="commissionRatio">
              <el-input type="number" v-model="form.commissionRatio" style="width: 350px;" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="text" @click="cancel">取消</el-button>
            <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
          </div>
        </el-dialog>

        <!--表格渲染-->
      	<el-table :data="data" size="small" style="width: 100%;margin-bottom: 15px">
				  <el-table-column prop="agentName" label="成员名称"/>
				  <el-table-column prop="phone" label="手机号码"/>
          <el-table-column prop="contacts" label="联系人"/>
				  <el-table-column prop="address" label="联系地址"/>
				  <el-table-column prop="commissionRatio" label="分成比例（%）"/>
          <el-table-column prop="createTime" label="创建时间"/>
				  <el-table-column v-if="checkPermission(['admin', 'agentMember:edit', 'agentMember:del'])" 
													  label="操作" width="200px" align="center" fixed="right">
              <template slot-scope="scope">
                  <el-button size="mini" style="margin-right: 2px" 
                        v-permission="['admin', 'agentMember:edit']"
                        icon="el-icon-check" type="primary" @click="showEditFormDialog(scope.row)">
                    编辑
                  </el-button>
                  <!--
                  <el-button size="mini" style="margin-right: 2px" 
                        v-permission="['admin', 'agentMember:del']"
                        icon="el-icon-check" type="danger" @click="delete1(scope.row)">
                    删除
                  </el-button>
                  -->
              </template>
				  </el-table-column>
				</el-table>
    </div>
  </div>
</template>

<script>
import { getAllMemberUser } from '@/api/memberUser'
import checkPermission from '@/utils/permission'
import crud from '@/mixins/crud'
import crudMemberDetail from '@/api/agentMember'
import { edit, del } from '@/api/agentMember'

export default {
  mixins: [crud],
  data() {
    return {
      title: '成员详情',
      users: [],
      crudMethod: { ...crudMemberDetail },
      form: {id: null, pid: null, isDel: null, createTime: null, updateTime: null, agentName: null, phone: null, address: null, contacts: null, imgUrl: null, commissionRatio: null},
      rules: {
          agentName: [
              { required: true, message: '成员名称不能为空', trigger: 'blur' },
              { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
          ], 
          phone: [
              { required: true, message: '手机号不能为空', trigger: 'blur' },
              { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ], 
          commissionRatio: [
              { required: true, message: '分成比例不能为空', trigger: 'blur' },
              { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
          ]
      }   
    }
  },
  created() {
    this.id = this.$route.params.id
  },
  mounted() {
    this.$nextTick(function(){
        this.getMemberUser()
    }) 
  },
  methods: {
    checkPermission,
    getMemberUser() {
        getAllMemberUser().then(res => {
            this.users = res
        }).catch(() => { })
    },
    // 获取数据前设置好接口地址
    beforeInit() {
      this.url = 'api/agentMember'
      this.params['pid'] = this.id
      return true
    },
    cancel() {
      this.resetForm2()
    },
    doSubmit() {
      this.loading = true
      this.doEdit()
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm2()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.init();
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm2() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: null, pid: null, createTime: null, updateTime: null, 
        agentName: null, phone: null, address: null, 
        contacts: null, imgUrl: null, commissionRatio: null
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
